/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021.
 * Description: support softlockup_grading feature
 * Author: shixin <shixin21@huawei.com>
 * Create: 2021-12-21
 */

#include <asm/irq_regs.h>
#include <linux/notifier.h>
#include <linux/sched/debug.h>
#include <linux/mutex.h>
#include <linux/sysctl.h>

#ifdef CONFIG_RTOS_KBOX
#include <linux/set_kbox_region.h>
#endif

#define GRADING_VAL_LIMIT		10
#define SOFTLOCKUP_GRADING_CLOSE	0
#define SOFTLOCKUP_GRADING_MAX		5
/* grading num belong to { 0, 1, 2, 3, 4 } */
static unsigned int grading_min = SOFTLOCKUP_GRADING_CLOSE;
static unsigned int grading_max = SOFTLOCKUP_GRADING_MAX - 1;
static unsigned int softlockup_record_grading;
/* val >= 10 / (grading + 1), val is integer */
static unsigned int softlockup_grading_val_tbl[SOFTLOCKUP_GRADING_MAX] = { 10, 5, 4, 3, 2 };
static DEFINE_PER_CPU(unsigned int, softlockup_record_cur_val);
static DEFINE_MUTEX(softlockup_record_mutex);
static ATOMIC_NOTIFIER_HEAD(rtos_softlockup_grading_chain);

int rtos_softlockup_grading_notifier_register(struct notifier_block *nb)
{
	if (nb == NULL) {
		pr_err("Invalid parameter, softlockup grading notifier register fail\n");
		return -EINVAL;
	}

	if (nb->notifier_call == NULL) {
		pr_err("notifier_call is NULL, softlockup grading notifier register fail\n");
		return -EINVAL;
	}

	return atomic_notifier_chain_register(&rtos_softlockup_grading_chain, nb);
}
EXPORT_SYMBOL_NS(rtos_softlockup_grading_notifier_register, HW_RTOS_NS);

int rtos_softlockup_grading_notifier_unregister(struct notifier_block *nb)
{
	if (nb == NULL) {
		pr_err("softlockup grading notifier unregister fail\n");
		return -EINVAL;
	}

	return atomic_notifier_chain_unregister(&rtos_softlockup_grading_chain, nb);
}
EXPORT_SYMBOL_NS(rtos_softlockup_grading_notifier_unregister, HW_RTOS_NS);

void reset_softlockup_record_cur_val(void)
{
	__this_cpu_write(softlockup_record_cur_val, 0);
}

static void show_notice_times(void)
{
	unsigned int cur_times = 0;
	unsigned int set_times = softlockup_record_grading + 1;
	unsigned int cur_val = __this_cpu_read(softlockup_record_cur_val);
	unsigned int set_val = softlockup_grading_val_tbl[softlockup_record_grading];

	if (cur_val == 0)
		cur_times = set_times;
	else if (set_val != 0)
		cur_times = cur_val / set_val;

	pr_info("[CPU#%d] softlockup grading [ %u / %u ]\n", smp_processor_id(), cur_times, set_times);
}

static int is_record(void)
{
	unsigned int cur_val = __this_cpu_read(softlockup_record_cur_val);
	unsigned int set_val = softlockup_grading_val_tbl[softlockup_record_grading];

	if (cur_val == 0) {
		pr_warn("SOFTLOCKUP_GRADING: Invalid val.\n");
		return 0;
	}

	if (cur_val >= GRADING_VAL_LIMIT) {
		pr_warn("SOFTLOCKUP_GRADING: Out range.\n");
		return 0;
	}

	if (cur_val % set_val != 0)
		return 0;

	return 1;
}

static void softlockup_record_process(void)
{
	struct pt_regs *regs = get_irq_regs();

#ifdef CONFIG_RTOS_KBOX
	int msgd;
	msgd = set_kbox_region(deadlock_region_id);
#endif

	if (regs)
		show_regs(regs);
	else
		dump_stack();

	show_lock_info(current, 0);

	atomic_notifier_call_chain(&rtos_softlockup_grading_chain, 0, NULL);

#ifdef CONFIG_RTOS_KBOX
	restore_kbox_region(msgd);
#endif
}

void softlockup_grading_handle(int duration)
{
	if (!softlockup_record_grading)
		return;

	if (__this_cpu_read(softlockup_record_cur_val) < GRADING_VAL_LIMIT)
		__this_cpu_inc(softlockup_record_cur_val);

	if (unlikely(duration)) {
		reset_softlockup_record_cur_val();
		show_notice_times();
	} else if (is_record()) {
		softlockup_record_process();
		show_notice_times();
	}
}

static int reset_softlockup_record(void *data)
{
	reset_softlockup_record_cur_val();
	return 0;
}

static int proc_softlockup_record_grading(struct ctl_table *table, int write,
					void __user *buffer, size_t *lenp, loff_t *ppos)
{
	int ret;
	int cpu;

	mutex_lock(&softlockup_record_mutex);

	if (write == 0) {
		ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
		goto out;
	}

	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
	if (ret) {
		goto out;
	}

	if (softlockup_record_grading == 0) {
		for (cpu = 0; cpu < nr_cpu_ids; cpu++)
			smp_call_on_cpu(cpu, reset_softlockup_record, NULL, false);
	}

out:
	mutex_unlock(&softlockup_record_mutex);
	return ret;
}

static struct ctl_table kernel_tbl[] = {
	{
		.procname       = "softlockup_grading",
		.data           = &softlockup_record_grading,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0640,
		.proc_handler   = proc_softlockup_record_grading,
		.extra1         = &grading_min,  /* 0, close */
		.extra2         = &grading_max,
	},
	{}
};

/* Define the parent directory /proc/sys/kernel. */
static struct ctl_table sys_table[] = {
	{
		.procname   = "kernel",
		.mode       = 0555,
		.child      = kernel_tbl
	},
	{}
};

int softlockup_grading_proc_init(void)
{
	if (register_sysctl_table(sys_table) == NULL)
		return -ENOMEM;

	return 0;
}

